package com.example.maze;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyDialogFragment extends DialogFragment {
	
	protected static final String NAME_KEY = "edity";
	protected static final String PERCENT_KEY = "percent";
	protected static final String SIZE_KEY = "size";
	protected static final String MOVES_KEY = "moves";
	protected static final String TIME_KEY = "time";
	protected static final String TYPE_KEY = "type";
	public EditText firstEdity;
	public static String edity;
	private int percent;
	private int moves;
	private int size;
	private int time;
	private int type;
	private GameMode gameMode;
	
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    	
    	LinearLayout linny = new LinearLayout(getActivity());
    	TextView view2 = new TextView(getActivity());
		view2.setText("You Win!");
		firstEdity = new EditText(getActivity());
		firstEdity.getText().toString();
		linny.addView(view2);
		linny.addView(firstEdity);
		
		if(savedInstanceState != null){
			String editString = savedInstanceState.getString("name");
			firstEdity.setText(editString);
		}
		
		
		percent = getArguments().getInt(MazeActivity.PERCENT_KEY);
		size = getArguments().getInt(MazeActivity.SIZE_KEY);
		moves = getArguments().getInt(MazeActivity.MOVES_KEY);
		time = getArguments().getInt(MazeActivity.TIME_KEY);
		type = getArguments().getInt(MazeActivity.TYPE_KEY);
		
		//getting the values from the gamemode enums to show the choice of modes
		GameMode[] values = GameMode.values();
		gameMode = values[type];

        return new AlertDialog.Builder(getActivity())
        
                .setTitle("Congratulations!")
                .setView(linny)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
  
                		edity = firstEdity.getText().toString();
                		
                		//filling in the info of the player's game to go in the score
                		Score score = new Score(size, percent, gameMode, time, moves);
                		PlayerScore newScore = new PlayerScore(edity, score);
                		
                		//getting hold of the tables to add data
                		LeaderBoardDataSource leaderBoardData = new LeaderBoardDataSource(getActivity());
                		leaderBoardData.open();
                		leaderBoardData.addPlayerScore(newScore);
                		leaderBoardData.close();
                		Intent i = new Intent(getActivity(), LeaderBoardActivity.class);
                		startActivity(i);

                    }
                })
                .create();
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
    	String str = firstEdity.getText().toString();
    	outState.putString("name", str);
    	super.onSaveInstanceState(outState);
    }
}
